/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Augusto Wolf
 */
public class MinhaTabela extends JTable {

    ArrayList<ArrayList<Color>> corFundo = new ArrayList<>();
    ArrayList<ArrayList<Color>> corFonte = new ArrayList<>();

    public MinhaTabela() {
        super();
        setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected, boolean hasFocus, int r, int c) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, r, c);
                setBackground(corFundo.get(r).get(c));
                setForeground(corFonte.get(r).get(c));
                return this;
            }
        });
    }

    @Override
    public DefaultTableModel getModel() {
        return (DefaultTableModel) super.getModel();
    }

    public void setRowCount(int rowCount) {
        ArrayList<ArrayList<Color>> antesFundo = new ArrayList<>();
        ArrayList<ArrayList<Color>> antesFonte = new ArrayList<>();
        for (int i = 0; i < getRowCount(); i++) {
            ArrayList<Color> aaFundo = new ArrayList<>();
            ArrayList<Color> aaFonte = new ArrayList<>();
            for (int j = 0; j < getColumnCount(); j++) {
                aaFundo.add(corFundo.get(i).get(j));
                aaFonte.add(corFonte.get(i).get(j));
            }
            antesFundo.add(aaFundo);
            antesFonte.add(aaFonte);
        }
        corFundo = new ArrayList<>();
        corFonte = new ArrayList<>();
        int i = 0;
        for (; i < rowCount && i < getRowCount(); i++) {
            corFundo.add(antesFundo.get(i));
            corFonte.add(antesFundo.get(i));
        }
        for (; i < rowCount; i++) {
            ArrayList<Color> aaFundo = new ArrayList<>();
            ArrayList<Color> aaFonte = new ArrayList<>();
            for (int j = 0; j < getColumnCount(); j++) {
                aaFundo.add(Color.WHITE);
                aaFonte.add(Color.WHITE);
            }
            antesFundo.add(aaFundo);
            antesFonte.add(aaFonte);
        }
        getModel().setRowCount(rowCount);
    }

    public void setColumnCount(int columnCount) {
        ArrayList<ArrayList<Color>> antesFundo = new ArrayList<>();
        ArrayList<ArrayList<Color>> antesFonte = new ArrayList<>();
        for (int i = 0; i < getRowCount(); i++) {
            ArrayList<Color> aaFundo = new ArrayList<>();
            ArrayList<Color> aaFonte = new ArrayList<>();
            for (int j = 0; j < getColumnCount(); j++) {
                aaFundo.add(corFundo.get(i).get(j));
                aaFonte.add(corFonte.get(i).get(j));
            }
            antesFundo.add(aaFundo);
            antesFonte.add(aaFonte);
        }
        corFundo = new ArrayList<>();
        corFonte = new ArrayList<>();
        for (int i = 0; i < getRowCount(); i++) {
            ArrayList<Color> aaFundo = new ArrayList<>();
            ArrayList<Color> aaFonte = new ArrayList<>();
            int j = 0;
            for (; j < columnCount && j < getColumnCount(); j++) {
                aaFundo.add(antesFundo.get(i).get(j));
                aaFonte.add(antesFonte.get(i).get(j));
            }
            for (; j < columnCount; j++) {
                aaFundo.add(Color.WHITE);
                aaFonte.add(Color.WHITE);
            }
            corFundo.add(aaFundo);
            corFonte.add(aaFonte);
        }

        getModel().setColumnCount(columnCount);
    }

    public boolean setCor(int row, int column, Color backgorund, Color foreground) {
        try {
            ArrayList<Color> aaFundo = corFundo.get(row);
            aaFundo.set(column, backgorund);
            corFundo.set(row, aaFundo);

            ArrayList<Color> aaFonte = corFonte.get(row);
            aaFonte.set(column, foreground);
            corFonte.set(row, aaFonte);

            setValueAt(getValueAt(row, column), row, column);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public void addRow(ArrayList rowData) {
        getModel().addRow(rowData.toArray());
    }

    public void addColumn(Object columnModel) {
        getModel().addColumn(columnModel);
    }

    public void clear() {
        setRowCount(0);
        setColumnCount(0);
    }
}
